﻿Public Class frm_ABM_Bancos
    Dim string_conexion As String
    Dim _idBanco As Integer
    Enum estado
        insertar
        modificar
        borrar
    End Enum
    Dim accion As estado = estado.insertar
    Dim _accesoBD As New AccesoBD
    Private Sub cmd_salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_salir.Click
        Me.Close()

    End Sub
    Private Sub cmd_aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_aceptar.Click
        If validar() Then
            If Me.accion = estado.insertar Then
                insertar()
            Else
                If Me.accion = estado.modificar Then
                    modificar(_idBanco)
                End If                
            End If
            MessageBox.Show("Se grabo con éxito", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.cargo_tabla()
            Me.txt_nombre.Text = ""
            Me.grupo_boton.Enabled = False
            Me.cmd_aceptar.Enabled = False
        End If
    End Sub

    Private Sub insertar()
        Dim consulta As String = ""

        consulta = "INSERT INTO Bancos values ('" & Me.txt_nombre.Text & "')"
        _accesoBD._insertar(consulta)        
    End Sub

    Private Function validar() As Boolean
        If Me.txt_nombre.Text = "" Then
            MessageBox.Show("Debe ingresar un nombre para el Banco", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txt_nombre.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Sub modificar(ByVal id As Integer)

        Dim modificacion As String = ""

        modificacion = "UPDATE Bancos "
        modificacion &= "SET nombre = '" & Me.txt_nombre.Text & "'"
        modificacion &= " WHERE idBanco = " & id

        _accesoBD._modificar(modificacion)
    End Sub
    Private Sub cmd_nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_nuevo.Click                
        grupo_boton.Enabled = True
        cmd_aceptar.Enabled = True
        txt_nombre.Text = ""
        txt_nombre.Focus()
        Me.accion = estado.insertar
    End Sub

    Private Sub frm_titulos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        grupo_boton.Enabled = False
        cmd_aceptar.Enabled = False
        cargo_tabla()
    End Sub
    Private Sub cargo_tabla()        
        Dim consulta As String = ""
        Dim tabla As New Data.DataTable
        
        consulta = "SELECT * FROM Bancos"

        tabla = _accesoBD.ejecutaSQL(consulta)

        dgv_banco.Rows.Clear()
        Dim c As Integer
        For c = 0 To tabla.Rows.Count - 1
            Me.dgv_banco.Rows.Add(tabla.Rows(c)("idBanco"), tabla.Rows(c)("nombre"))
        Next
    End Sub
    Private Sub borrar(ByVal id As Integer)
        Dim consulta As String
        consulta = "DELETE FROM Bancos WHERE idBanco = " & id
        _accesoBD._eliminar(consulta)
        cargo_tabla()
    End Sub
    Private Sub ModificarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Me.accion = estado.modificar
        _idBanco = dgv_banco.CurrentRow.Cells("col_id").Value
        Me.mostrarDatos(_idBanco)
        grupo_boton.Enabled = True
        Me.cmd_aceptar.Enabled = True
    End Sub

    Private Sub mostrarDatos(ByVal idBanco As Integer)
        Dim consulta As String
        Dim tabla As DataTable

        consulta = "select nombre from Bancos where idBanco = " & idBanco
        tabla = _accesoBD.ejecutaSQL(consulta)

        Me.txt_nombre.Text = tabla.Rows(0)("nombre")
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        _idBanco = dgv_banco.CurrentRow.Cells("col_id").Value
        If MessageBox.Show("Esta seguro que desea eliminar el registro", "Importante", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.borrar(_idBanco)          
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        txt_nombre.Text = ""
        grupo_boton.Enabled = False
    End Sub

End Class